/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package qlvbdq.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import qlvbdq.dconnection.SQLServerConnect;
import qlvbdq.dto.BaoCaoTonDTO;

/**
 *
 * @author tuan
 */
public class BaoCaoTonKhoDAO {
    private static final String MaBaoCaoTon = "MaBCTon0";
    private static final int lengthMa = 8;
    
    public BaoCaoTonKhoDAO() {
    }
    
    public String getNewMaBaoCaoTon() throws SQLException {
        String MaBCTon = "";
        int maxMa = 0;
        ResultSet rs = SQLServerConnect.getInstance().getDataFromStore("GetAllBAOCAOTONFromDataBase", null);
        while (rs.next()) {
            int temp = Integer.parseInt( rs.getString("MABAOCAOTON").substring(lengthMa) );
            if (maxMa < temp) {
                maxMa = temp;
            }
        }
        maxMa ++;
        MaBCTon = MaBCTon.concat(MaBaoCaoTon + maxMa);
        return MaBCTon;
    }
    
    public BaoCaoTonDTO getBaoCaoTonByMaBCDT(String MaBCTon) throws SQLException {
        Object[] objs = { MaBCTon };
        ResultSet result = SQLServerConnect.getInstance().getDataFromStore("GetBAOCAOTONFromDataBase( ? )", objs);
        BaoCaoTonDTO baocaoDT = new BaoCaoTonDTO();
        while (result.next()) {
            baocaoDT.setMaBaoCaoTon(result.getString("MABAOCAOTON"));
            baocaoDT.setNgayBaoCao(result.getDate("NGAYBAOCAO"));
        }
        return baocaoDT;
    }
    
    public ArrayList<BaoCaoTonDTO> getBaoCaoTonInTime(Date timeStart, Date timeEnd) throws SQLException {
        ArrayList<BaoCaoTonDTO> listBaoCao = new ArrayList<>();
        Object[] objs = { timeStart, timeEnd };
        ResultSet result = SQLServerConnect.getInstance().getDataFromStore("GetBAOCAOTONInTimeFromDataBase( ?, ? )", objs);
        BaoCaoTonDTO baocaoDT = new BaoCaoTonDTO();
        while (result.next()) {
            baocaoDT.setMaBaoCaoTon(result.getString("MABAOCAOTON"));
            baocaoDT.setNgayBaoCao(result.getDate("NGAYBAOCAO"));
            listBaoCao.add(baocaoDT);
        }
        return listBaoCao;
    }
    
    public int updateBaoCaoTon(BaoCaoTonDTO baocaoDT) {
        Object[] objs = { baocaoDT.getMaBaoCaoTon(), baocaoDT.getNgayBaoCao()};
        return SQLServerConnect.getInstance().updateDataToDatabase("UpdateBAOCAOTONFromDataBase(?, ?)", objs);
    }
    
    public int deleteBaoCaoTonByMaBC(String MaBC) {
        Object[] objs = { MaBC };
        return SQLServerConnect.getInstance().updateDataToDatabase("DeleteBAOCAOTONFromDataBase( ? )", objs);
    }
    
    
    public int deleteAllBaoCaoTon() {
        return SQLServerConnect.getInstance().updateDataToDatabase("DeleteAllBAOCAOTONFromDataBase", null);
    }
    public int insertBaoCaoTon(BaoCaoTonDTO baocaoDT) {
        Object[] objs = { baocaoDT.getMaBaoCaoTon(), baocaoDT.getNgayBaoCao()};
        return SQLServerConnect.getInstance().updateDataToDatabase("InsertBAOCAOTONFromDataBase(?, ?)", objs);
    }        
}
